import java.awt.event.*;
import java.awt.Dimension;
import javax.swing.*;
import javax.media.opengl.*;
import javax.media.opengl.awt.*;  
import javax.media.opengl.glu.*;
import com.jogamp.opengl.util.gl2.GLUT; 

public class WireframeOPENGL  extends glskeleton
                          implements GLEventListener, KeyListener 
                          {

    private GLUT glut;
    
    float rotX = 0;
    float rotY = 0;
    float rotZ = 0;
    boolean perspective = false;
    
    public static void main(String[] args) {
    
    
        final javax.media.opengl.awt.GLCanvas canvas = new javax.media.opengl.awt.GLCanvas();
        WireframeOPENGL demo = new WireframeOPENGL();
        demo.setCanvas(canvas);
        canvas.addGLEventListener(demo);
        demo.setDefaultListeners(demo);
        
        JFrame frame = new JFrame("Wireframe by OPENGL");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);          
        
        frame.getContentPane().add(canvas);
        frame.getContentPane().setPreferredSize(
            new Dimension(500, 500));
        frame.pack();
        frame.setLocationRelativeTo(null);              
        frame.setVisible(true);
        canvas.requestFocusInWindow();
    }
    
    /*
     * Initialize material property, light source, lighting model, and depth
     * buffer.
     */
    public void init(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        glut = new GLUT();
        gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    }
    
    
    public void display(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
        gl.glColor3f(0,0,0);
        
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        
      // apply projection matrix        
        if (perspective)
            gl.glFrustum(-0.75,0.75,-0.75,0.75,1,4.5);
        else
            gl.glOrtho(-1.5, 1.5, -1.5, 1.5, -10.0, 10.0);


        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();        
        
      // move object away from camera
        gl.glTranslatef(0,-.25f,-2.5f);    
        
      // do interactive rotations        
        gl.glRotatef(rotX,1,0,0);
        gl.glRotatef(rotY,0,1,0);
        gl.glRotatef(rotZ,0,0,1);
        
      // create and transform objects
        // first the tower
        gl.glPushMatrix();
        gl.glTranslatef(0.75f,0.25f,-0.75f);
        gl.glScalef(0.5f,1.5f,0.5f);     
        glut.glutWireCube(1.0f);  
        gl.glPopMatrix();
        
        // then the main building
        glut.glutWireCube(1.0f);
           
        gl.glFlush();
    }
    
    public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glViewport(0, 0, Math.min(w,h), Math.min(w,h));        
    }
    
    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
                               boolean deviceChanged) {
    }
    
    public void keyTyped(KeyEvent key) {
    }
    
    public void dispose(javax.media.opengl.GLAutoDrawable dr) {
    }        
    
    public void keyPressed(KeyEvent key) {
        switch (key.getKeyCode()) {
            case KeyEvent.VK_ESCAPE:
                super.runExit();
                break;
            case KeyEvent.VK_P:
               perspective = true;
                refresh();
               break;
            case KeyEvent.VK_N:
               perspective = false;
                refresh();
               break;               
            case KeyEvent.VK_LEFT:
                rotY -= 3;
                refresh();
                break;  
            case KeyEvent.VK_RIGHT:
                rotY += 3;
                refresh();
                break;  
            case KeyEvent.VK_UP:
                rotX -= 3;
                refresh();              
                break;  
            case KeyEvent.VK_DOWN:
                rotX += 3;
                refresh();
                break;  
            case KeyEvent.VK_PAGE_UP:
                rotZ += 3;
                refresh();
                break;
            case KeyEvent.VK_PAGE_DOWN:
                rotZ -= 3;
                refresh();
                break;                                                                                             
            default:
                System.out.println(key.getKeyCode());
                break;
        }
    }
    
    public void keyReleased(KeyEvent key) {
    }
    
}

